/* petclub.c -- 使用二叉树 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "BinarySearchTree.h"

char menu(void);
void addpet(Tree * pt);
void droppet(Tree * pt);
void showpets(const Tree * pt);
void findpets(const Tree * pt);
void printitem(Item item);
void uppercase(char * str);

int main(int argc, char *argv[]) {

    Tree pets;
    char choice;

    InitializeTree(&pets);
    while ((choice = menu()) != 'q') {
        switch (choice) {
            case 'a':
                addpet(&pets);
                break;
            case 'l':
                showpets(&pets);
                break;
            case 'f':
                findpets(&pets);
                break;
            case 'n':
                printf("%d pets in club\n", TreeItemCount(&pets));
                break;
            case 'd':
                droppet(&pets);
                break;

            default:
                puts("Switching error");
        }
    }
    DeleteAll(&pets);
    puts("Bye.");
    return 0;
}

char menu(void) {
    int ch;

    puts("Nerfville Pet Club Membership Program");
    puts("Enter the letter coresponding your choice: ");
    puts("a) add a pet        l) show list of pets");
    puts("n) number of pet    f) find pets");
    puts("d) delete a pet     q) quit");
    while ((ch = getchar()) != EOF) {
        while (getchar() != '\n')  /* 清空缓冲 */
            continue;
        ch = tolower(ch);
        if (strchr("alrfndq", ch) == NULL)
            puts("Please enter an a, l, f, n, d, or q");
        else
            break;
    }
    if (ch == EOF)    // 令EOF导致程序退出
        ch = 'q';

    return ch;
}

void addpet(Tree * pt) {
    Item temp;

    if (TreeIsFull(pt))
        puts("No room in the club!");
    else {
        puts("Please enter name of pets: ");
        gets(temp.petname);
        puts("Please enter pet kind: ");
        gets(temp.petkind);
        uppercase(temp.petname);
        uppercase(temp.petkind);
        AddItem(&temp, pt);
    }
}

void droppet(Tree * pt) {
    Item temp;

    if (TreeIsEmpty(pt)) {
        puts("No entries!");
        return;
    }

    puts("Please enter name of pet you wish to delete: ");
    gets(temp.petname);
    puts("Please enter pet kind: ");
    gets(temp.petkind);
    printf("%s the %s ", temp.petname, temp.petkind);
    if (DeleteItem(&temp, pt))
        printf("is dropped from the club.\n");
    else
        printf("is not a member.\n");
}

void showpets(const Tree * pt) {
    if (TreeIsEmpty(pt))
        puts("No entries!");
    else
        Traverse(pt, printitem);
}

void findpets(const Tree * pt) {
    Item temp;

    if (TreeIsEmpty(pt)) {
        puts("No entries!");
        return;
    }

    puts("Please enter name of pet you wish to delete: ");
    gets(temp.petname);
    puts("Please enter pet kind: ");
    gets(temp.petkind);
    uppercase(temp.petname);
    uppercase(temp.petkind);
    printf("%s the %s ", temp.petname, temp.petkind);
    if (InTree(&temp, pt))
        printf("is a member.\n");
    else
        printf("is not a member.\n");

}

void printitem(Item item) {
    printf("Pet: %-19s Kind: %-19s\n", item.petname, item.petkind);
}

void uppercase(char * str) {
    while (*str) {
        *str = toupper(*str);
        str++;
    }
}
